package com.zoe.wan.android.example.activity.tab

import android.graphics.BitmapFactory
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.zoe.wan.android.example.BR
import com.zoe.wan.android.example.R
import com.zoe.wan.android.example.databinding.ActivityTabBinding
import com.zoe.wan.android.example.fragment.home.FragHome
import com.zoe.wan.android.example.fragment.hotkey.FragHotkey
import com.zoe.wan.android.example.fragment.knowledge.FragKnowledge
import com.zoe.wan.android.example.fragment.personal.FragPersonal
import com.zoe.wan.android.example.repositry.Repository
import com.zoe.wan.base.BaseActivity
import com.zoe.wan.base.adapter.Pager2Adapter
import com.zoe.wan.base.tab.NavigationBottomBar

class TabActivity() : BaseActivity<ActivityTabBinding, TabViewModel>() {
    override fun getLayoutId(): Int {
        return R.layout.activity_tab
    }

    override fun getViewModelId(): Int {
        return BR.tabVm
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        Repository.init(this)
    }

    override fun initViewData() {

        initPage()

        /// 选中图片
        val selectIcon = arrayOf(
            BitmapFactory.decodeResource(resources, R.drawable.icon_home_selected),
            BitmapFactory.decodeResource(resources, R.drawable.icon_hot_key_selected),
            BitmapFactory.decodeResource(resources, R.drawable.icon_knowledge_selected),
            BitmapFactory.decodeResource(resources, R.drawable.icon_personal_selected)
        )
        /// 未选中图片
        val normalIcon = arrayOf(
            BitmapFactory.decodeResource(resources, R.drawable.icon_home_grey),
            BitmapFactory.decodeResource(resources, R.drawable.icon_hot_key_grey),
            BitmapFactory.decodeResource(resources, R.drawable.icon_knowledge_grey),
            BitmapFactory.decodeResource(resources, R.drawable.icon_personal_grey)
        )
        // bottomBar标题数组
        val tabText = arrayOf("首页","热点","信息","个人")

        /// 设置按钮类型 也可以不用设置
        val tabType = arrayOf(
            NavigationBottomBar.BOTTOM_TAB_TYPE_BOTH_HAVE,
            NavigationBottomBar.BOTTOM_TAB_TYPE_BOTH_HAVE,
            NavigationBottomBar.BOTTOM_TAB_TYPE_BOTH_HAVE,
            NavigationBottomBar.BOTTOM_TAB_TYPE_BOTH_HAVE)
        binding?.tabBottomBar?.setSelectedIcons(selectIcon.toList())
        binding?.tabBottomBar?.setUnselectIcons(normalIcon.toList())
        binding?.tabBottomBar?.setTabType(tabType.toList())
        binding?.tabBottomBar?.setTabText(tabText.toList())

        binding?.tabBottomBar?.setupViewpager(binding?.tabViewPage2)
        binding?.tabBottomBar?.start()

        binding?.tabBottomBar?.registerTabClickListener(object :NavigationBottomBar.OnBottomTabClickListener{
            override fun tabClick(position: Int) {

            }
        })
    }

    private fun initPage(){
        val tabPage = mutableListOf<Fragment>()
        tabPage.add(FragHome())
        tabPage.add(FragHotkey())
        tabPage.add(FragKnowledge())
        tabPage.add(FragPersonal())

        val page2Adapter = Pager2Adapter(this)
        page2Adapter.setData(tabPage)
        binding?.tabViewPage2?.offscreenPageLimit = ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT
        binding?.tabViewPage2?.adapter = page2Adapter
    }

}
